package com.wang.API;

import com.wang.bean.*;
import com.wang.config.ShiroUtils;
import com.wang.service.SinglechoiceResultsService;
import com.wang.service.TestResultsService;
import com.wang.service.TestService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 考试类
 */
@RestController
@RequestMapping("api")
public class TestResultsController {

    @Autowired
    private TestResultsService testResultsService;

    @Autowired
    private TestService testService;

    @RequestMapping(value = "/submittestresults")
    @RequiresPermissions("sys:task:submittestresults")
    public Result submittestresults(@RequestBody TestResults tr){
        Result result = new Result();
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        if (userEntity == null){
            result.setCode("108");
            return result;
        }
        if (!(userEntity.getUser_id()).equals(tr.getUser_id())){
            result.setCode("808");
            return result;
        }
        SinglechoiceSubmissionLog sLog = new SinglechoiceSubmissionLog();
        sLog.setSinglechoice_id(tr.getSinglechoice_id());
        sLog.setUser_id(tr.getUser_id());
        sLog.setUser_option(tr.getUser_option());
        sLog.setSubmissiontime(new Date());
        sLog.setExamination_id(tr.getExamination_id());
        //singlechoiceResultsService.addsinglechoicesubmissionlog(sLog);
        try{
            //检查试卷是否存在
            if (tr.getExamination_id() == null || "".equals(tr.getExamination_id())){
                result.setCode("807");
                sLog.setCode("807");
                sLog.setMessage(result.getMessage());
                testResultsService.addsinglechoicesubmissionlog(sLog);
                return result;
            }
            //检查选项
            if (tr.getUser_option() == null || "".equals(tr.getUser_option())){
                result.setCode("803");
                sLog.setCode("803");
                sLog.setMessage(result.getMessage());
                testResultsService.addsinglechoicesubmissionlog(sLog);
                return result;
            }

            //检查用户和题目
            if (tr.getUser_id() == null || tr.getUser_id() == 0 || tr.getSinglechoice_id() == null || tr.getSinglechoice_id() == 0){
                result.setCode("804");
                sLog.setCode("804");
                sLog.setMessage(result.getMessage());
                testResultsService.addsinglechoicesubmissionlog(sLog);
                return result;
            }

            //检查该选择题是否存在于数据库
            TestPaper testPaper = new TestPaper();
            testPaper.setExamination_id(tr.getExamination_id());
            testPaper.setSinglechoice_id(tr.getSinglechoice_id());
            if (testService.findOneTestPaperByExaminationIdSinglechoiceId(testPaper) == null){
                result.setCode("805");
                sLog.setCode("805");
                sLog.setMessage(result.getMessage());
                testResultsService.addsinglechoicesubmissionlog(sLog);
                return result;
            }

            if (testResultsService.selectOneById(tr.getUser_id()) == null){
                result.setCode("806");
                sLog.setCode("806");
                sLog.setMessage(result.getMessage());
                testResultsService.addsinglechoicesubmissionlog(sLog);
                return result;
            }
            if (testResultsService.findTestPaperResultsByUserSinglechoiceIdExaminationId(tr) != null){
                testResultsService.updateTestPaperResults(tr);
                result.setCode("802");
                sLog.setCode("802");
                sLog.setMessage(result.getMessage());
                testResultsService.addsinglechoicesubmissionlog(sLog);
                return result;
            } else {
                testResultsService.addTestPaperResults(tr);
                result.setCode("801");
                sLog.setCode("801");
                sLog.setMessage(result.getMessage());
                testResultsService.addsinglechoicesubmissionlog(sLog);
                return result;
            }
        } catch (Exception e){
            System.out.println(e);
            result.setCode("800");
            sLog.setCode("800");
            sLog.setMessage(result.getMessage());
            testResultsService.addsinglechoicesubmissionlog(sLog);
            return result;
        }
    }

}
